//
//  NewsTableViewCell.m
//  item-NetEaseNews-tyw
//
//  Created by tyw on 15/9/30.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "NewsTableViewCell.h"
#import "NetEaseImgView.h"
#import "UIImageView+WebCache.h"

#define newsImgWidth 80
#define photosImgWidth kWidth/3-10

@implementation NewsTableViewCell {
    //三张图片，如果是图集，就并列显示。如果是新闻，就选第一张，其余两张隐藏;如果是图集，第一张就还是用原来的，另两张是headNewsImgArray数组中的前两张
    NetEaseImgView* imgView1;
    NetEaseImgView* imgView2;
    NetEaseImgView* imgView3;
    
    //标题
    UILabel* titleLabel;
    //新闻简介,如果是图集就隐藏
    UILabel* contentLabel;
    
    //跟帖
    UIImageView* replyImgView;
    UILabel* replyLabel;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.userInteractionEnabled = YES;
        
        imgView1 = [[NetEaseImgView alloc] init];
//        imgView1.contentMode = UIViewContentModeScaleAspectFit;
//        imgView1.backgroundColor = [UIColor redColor];
        
        imgView2 = [[NetEaseImgView alloc] init];
//        imgView2.contentMode = UIViewContentModeScaleAspectFit;
//        imgView2.backgroundColor = [UIColor greenColor];
        
        imgView3 = [[NetEaseImgView alloc] init];
//        imgView3.contentMode = UIViewContentModeScaleAspectFit;
//        imgView3.backgroundColor = [UIColor blueColor];
        
        titleLabel = [[UILabel alloc] init];
        titleLabel.textColor = [UIColor blackColor];
        titleLabel.font = [UIFont systemFontOfSize:14];
        
        contentLabel = [[UILabel alloc] init];
        contentLabel.textColor = [UIColor grayColor];
        contentLabel.numberOfLines = 2;
        contentLabel.font = [UIFont systemFontOfSize:12];
        
        replyImgView = [[UIImageView alloc] init];
        
        replyLabel = [[UILabel alloc] init];
        replyLabel.font = [UIFont systemFontOfSize:10];
        replyLabel.textAlignment = NSTextAlignmentCenter;
        
        [self.contentView addSubview:imgView1];
        [self.contentView addSubview:imgView2];
        [self.contentView addSubview:imgView3];
        [self.contentView addSubview:titleLabel];
        [self.contentView addSubview:contentLabel];
        [self.contentView addSubview:replyImgView];
        [self.contentView addSubview:replyLabel];
    }
    
    return self;
}

- (void)setNewsModel:(NewsModel *)newsModel {
    _newsModel = newsModel;
    
    [self setNeedsLayout];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    
#warning 在这里判断是图集还是新闻
    if (!_newsModel.photosetID.length) {      //是新闻
        //图
        [imgView1 setFrame:CGRectMake(10, 5, newsImgWidth, self.bounds.size.height-10)];
        
        imgView2.hidden = YES;
        imgView3.hidden = YES;
        
        
        //标题
        [titleLabel setFrame:CGRectMake(20+newsImgWidth, 10, kWidth-newsImgWidth-30, 20)];
        
        //新闻简介
        contentLabel.hidden = NO;
        [contentLabel setFrame:CGRectMake(20+newsImgWidth, 33, kWidth-newsImgWidth-30, self.bounds.size.height-40)];
        NSAttributedString* attrStr = [Tools attributedStrFromStr:_newsModel.content lineSpace:3];
        contentLabel.attributedText = attrStr;
        [contentLabel sizeToFit];
        
        //跟帖
        [replyImgView setFrame:CGRectMake(kWidth-60, self.bounds.size.height-25, 50, 15)];
        
    } else {            //是图集
        //图
        [imgView1 setFrame:CGRectMake(10, 40, photosImgWidth, self.bounds.size.height-50)];
        
        imgView2.hidden = NO;
        [imgView2 setFrame:CGRectMake(imgView1.frame.origin.x+5+photosImgWidth, 40, photosImgWidth, self.bounds.size.height-50)];
        NSString* img2Str = _newsModel.headNewsImgArray[0][@"imgsrc"];
        if (img2Str) {
            [imgView2 sd_setImageWithURL:[NSURL URLWithString:img2Str]];
        } else {
            [imgView2 setBgImg];
        }
        
        imgView3.hidden = NO;
        [imgView3 setFrame:CGRectMake(imgView2.frame.origin.x+5+photosImgWidth, 40, photosImgWidth, self.bounds.size.height-50)];
        NSString* img3Str = _newsModel.headNewsImgArray[1][@"imgsrc"];
        if (img3Str) {
            [imgView3 sd_setImageWithURL:[NSURL URLWithString:img3Str]];
        } else {
            [imgView3 setBgImg];
        }
        
        //标题
        [titleLabel setFrame:CGRectMake(10, 10, kWidth-60, 20)];
        
        //新闻简介
        contentLabel.hidden = YES;
        
        //跟帖
        [replyImgView setFrame:CGRectMake(kWidth-60, 10, 50, 15)];
    }
    
    
    //公共的信息
    //图片第一张
    if (_newsModel.imgUrl) {
        [imgView1 sd_setImageWithURL:[NSURL URLWithString:_newsModel.imgUrl]];
    } else {
        [imgView1 setBgImg];
    }
    //标题内容
    titleLabel.text = _newsModel.title;
    //跟帖
    UIImage* image = [UIImage imageNamed:@"cola_bubble_gray"];
    [image stretchableImageWithLeftCapWidth:10 topCapHeight:0];
    replyImgView.image = image;
    [replyLabel setFrame:replyImgView.frame];
    float replyCount = [_newsModel.replyCount floatValue];
    if (replyCount>9999) {
        replyLabel.text = [NSString stringWithFormat:@"%.1f万跟帖", replyCount/10000];
    } else {
        replyLabel.text = [NSString stringWithFormat:@"%.0f跟帖", replyCount];
    }
    
}

@end
